package com.fhnew.app.widget;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;


import com.fhnew.app.R;
import com.fhnew.app.util.HLog;

import static java.lang.Math.abs;

public class FloatButton extends View {

    private float touchStartLeft =0;
    private float touchStartTop = 0;
    private int startLeft = 0;
    private int startTop = 0;

    private int windowWidth = 0;
    private int windowHeight = 0;

    private boolean isMoved = false;

    private  OnClickListener onClickListener;

    //当移动范围超过当前值时才算移动
    private static final float OVER_CLICK_X = 10;
    private static final float OVER_CLICK_Y = 10;

    private ConstraintLayout constraintLayout;

    public FloatButton (Context context) {
        super(context);
        init();
    }

    public FloatButton (Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FloatButton (Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    private void init(){
        Resources resources = this.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        windowWidth = dm.widthPixels;
        windowHeight = dm.heightPixels;

//      constraintLayout = (ConstraintLayout) LayoutInflater.from(getContext()).inflate(R.layout.view_float_button,null);
//      addView(constraintLayout);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        float x =  event.getX();
        float y  = event.getY();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                touchStartLeft = x+getLeft();
                touchStartTop = y+getTop();
                startLeft = getLeft();
                startTop = getTop();
                isMoved = false;
                return true;
            case MotionEvent.ACTION_UP:
                float movedX = abs(x+getLeft() - touchStartLeft);
                float movedY = abs(y+getTop() - touchStartTop);
                if(isMoved && (OVER_CLICK_X < movedX || OVER_CLICK_Y < movedY)){
                    return  true;
                } else {
                    if(onClickListener!=null)
                        onClickListener.onClick(this);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                isMoved = true;
                int newX = (int)(getLeft()+x - touchStartLeft + startLeft);
                int newY = (int)(getTop()+y-touchStartTop+startTop);
                if(newX<0)newX = 0;
                if(newY<0) newY = 0;
                if(newX+getWidth()>windowWidth){
                    newX = windowWidth-getWidth();
                }
                if(newY+getHeight()>windowHeight){
                    newY  =windowHeight-getHeight();
                }
                layout(newX,newY ,newX+getWidth(),newY+getHeight());
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void setOnClickListener (OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
}
